package org.compiere.model;


import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Properties;
import java.util.logging.Level;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

public class Mhrmcheckoffline extends X_hrm_checkoff_line {
	
	/**	Static Logger	*/
	private static CLogger	s_log	= CLogger.getCLogger (Mhrmcheckoffline.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = 2709930239304521704L;

	public Mhrmcheckoffline(Properties ctx) {
		super(ctx);
		
	}
	
	public Mhrmcheckoffline(Properties ctx, int hrm_checkoff_line_ID,
			String trxName) {
		super(ctx, hrm_checkoff_line_ID, trxName);
		
	}

	public Mhrmcheckoffline(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
		
	}

	/**
	 * Search check off line
	 * 
	 * @param C_BPartner_ID
	 * @param hrm_checkoff_ID
	 * @return  Model check off line
	 */
	public static Mhrmcheckoffline getCheckoffline(int C_BPartner_ID, Timestamp dt) {
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		Mhrmcheckoffline chkoff = null;
			
		// 
		String sql_ = "SELECT * FROM HRM_CHECKOFF_LINE \n " +
						"WHERE ISACTIVE='Y' \n " +
						"AND AD_CLIENT_ID=" + Env.getAD_Client_ID(Env.getCtx()) + " \n " +
						"AND C_BPARTNER_ID=" + C_BPartner_ID + " \n " +
						"AND DATEFROM BETWEEN DATE_TRUNC('MONTH', " + DB.TO_DATE(dt, true) + ") " +
						"AND DATE_TRUNC('MONTH'," + DB.TO_DATE(dt, true) + ") + '1 MON'::INTERVAL - '1 DAY'::INTERVAL";						
			try
			{
				pstmt = DB.prepareStatement(sql_,null);
				rs = pstmt.executeQuery();	
				if(rs.next())
					chkoff = new Mhrmcheckoffline (Env.getCtx(), rs, null);
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}
			
	return chkoff;
	
	}		
}
